//@charset "utf-8";
// 板块请求
define(function (require, exports, module) {
    var BaseModel = require('../models/clientbase');
    var Util = external.CreateObject('Util');
    var Quote = external.CreateObject('Quote');
    var commonMethod = require('../lib/common');
    var PlateModel = BaseModel.extend({

    //获取列表
    requestList: function(callback){
      var result = Util.getCustomBlockList();
      var res = eval("("+ result +")");
      callback(res);
    },
    //新建板块 BACL: id  0为创建失败
    requestCreate: function(name,condition,callback){  // 普通板块condition为''

      var result = Util.addCustomBlock(name,condition);
      callback(result);
    },
    // 判断是否实时板块 BACK:true/false
    requestIsRealtime: function(id,callback){
      var result = Util.isSelectCustomBlock(id);
      callback(result);
    },
    // 重命名板块  BACK:true/false
    requestRename:function(id,newname,callback){
      var result = Util.renameCustomBlock(id,newname);
      callback(result);
    },
    // 增加股票到板块
    requestAdd: function(id,codelist,callback){
      var num = Util.addStockToCustomBlock(id,codelist);
      callback(num);
    },
    // 删除板块 BACK:true/false
    requestDelete: function(id,callback){
      var result = Util.deleteCustomBlock(id);
      callback(result);
    },
    jumpPlate: function(id){
      id = parseInt(id);
	  if (id == 33) {
		Quote.switchPage(392);//33为自选股，产品指定跳转到id为392的页面
	  } else {
		Quote.switchBlock(id);
	  }
    },
    // 处理板块命名
    formatPlateName:function(names,newname){
      newname = $.trim(newname);
      var index = commonMethod.arrIndexOf(names,newname);
      if(index === -1) {names.push(newname);return {newname:newname,platenames:names};}
      // else {names.splice(index,1);}
      var defaultname = this.getRepeatName(newname);
      var reg = new RegExp("^"+defaultname);
      var arrNum = [];
      $.each(names,function(i,name){
        if(reg.test(name)){
            var num = name.replace(defaultname,'');
            if(num.length === 0){
              arrNum[0] = 1;
            }
            num = parseInt(num);
            if(num){
              arrNum[num] = 1;
            }
        }
      });
      if(!arrNum[0]) {
        names.push(defaultname);
        return {newname:defaultname,platenames:names};
      }
      for(var i=1;i<=arrNum.length;i++){
        if(!arrNum[i]){
          names.push(defaultname+i);
          return {newname:defaultname+i,platenames:names};
        }
      }

    },
    getRepeatName: function(name){
      var lastIndex = name.length-1;
      if(!parseInt(name.substring(lastIndex,lastIndex+1)) ) {return name;}
      for(var i=name.length-2;i>=0;i--){
        if(!Number( name.substring(i,i+1) ) && Number( name.substring(i+1,i+2) )){
          lastIndex = i;
        }
      }
      name = name.substring(0,lastIndex+1);
      return name;
    },
    removePlateName: function(names,deletename){
      var platenames = commonMethod.arrRemove(names,deletename);
      return platenames;
    },
    //判断板块是否能够更新数据（是否能自动执行选股）
    isCustomBlockNeedUpdate : function(id){
      try{
          var status = Util.isCustomBlockNeedUpdate(id);
          if (status === false) {
            return 0;
          }
          return 1;
        } catch (e) {
          alert('该版本不支持');
        }
    },
    //设置板块更新状态
    enableCustomBlockUpdate : function(id, status){
        return Util.enableCustomBlockUpdate(id, status);
    },
    //获取板块条件
    getCustomBlockQuery : function (id) {
      return Util.getCustomBlockQuery(id);
    },
    //更新板块条件
    setCustomBlockQuery : function (id, query) {
      return Util.setCustomBlockQuery(id, query);
    },
    //获取是否提示添加动态板块
    isShowCreateQueryBlock : function () {
      return Util.isShowCreateQueryBlock();
    },
    //设置不再提示添加动态板块
    enableShowCreateQueryBlock : function (enable) {
      return Util.enableShowCreateQueryBlock(enable);
    },
    //客户端板块修改（增、删、改）的事件通知，客户端推送给网页
    registerCustomBlockUpdatePush : function (self, callback) {
      Util.registerCustomBlockUpdatePush({
        onready: function (rst){
          callback(self, rst);
        }
      })
    },
    //取消注册推送，关闭网页后应该调用此接口
    unregisterCustomBlockUpdatePush : function () {
      Util.unregisterCustomBlockUpdatePush();
    }
  });

  module.exports = new PlateModel();
});
